<template>
  <div class="login-container">
    <!-- 左边 -->
    <div class="logo" />
    <!-- 右边 -->
    <div class="form">
      <h1>智慧园区-登录</h1>
      <el-card shadow="never" class="login-card">
        <!--登录表单-->
        <el-form ref="loginRef" :model="loginForm" :rules="loginRules">
          <!-- 账号 -->
          <el-form-item label="账号" prop="username">
            <el-input v-model="loginForm.username" placeholder="请输入手机号" />
          </el-form-item>
          <!-- 密码 -->
          <el-form-item label="密码" prop="password">
            <el-input v-model="loginForm.password" placeholder="请输入密码" show-password />
          </el-form-item>
          <!-- 模型uuid -->
          <el-form-item label="模型uuid" prop="uuid">
            <el-input v-model="loginForm.uuid" placeholder="请输入模型uuid" show-password />
          </el-form-item>
          <el-form-item>
            <el-checkbox v-model="loginForm.isAgree">记住我</el-checkbox>
          </el-form-item>
          <el-form-item>
            <el-button style="width:350px" type="primary" @click="login">登录</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script>
import store from '@/store'

// import { loginApi } from '@/api/user'

export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: process.env.NODE_ENV === 'development' ? 'demo' : '', // demo
        password: process.env.NODE_ENV === 'development' ? 'Hmzs%001' : '', // Hmzs%001
        uuid: process.env.NODE_ENV === 'development' ? '66ea8d2a-7416-4be5-ba7e-c030cf88b3f3' : '', // 66ea8d2a-7416-4be5-ba7e-c030cf88b3f3
        isAgree: process.env.NODE_ENV === 'development'
      },
      loginRules: {
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 16, message: '密码长度应该为6-16位之间', trigger: 'blur' }
        ],
        uuid: [
          { required: true, message: '请输入uuid', trigger: 'blur' }
        ],
        isAgree: [
          {
            // validator: (rule, value, callback) => {
            //   value ? callback() : callback('没有勾选记住我')
            // }
          }
        ]
      }
    }
  },
  methods: {
    async  login() {
      if (this.loginForm.isAgree) {
        store.commit('user/SET_INFO', {
          username: this.loginForm.username,
          password: this.loginForm.password,
          uuid: this.loginForm.uuid
        })
      }
      try {
        // 整体校验
        await this.$refs.loginRef.validate()
        // const res = await loginApi('park/login', this.loginForm)
        // console.log(res)
        // 异步登录
        const { username, password } = this.loginForm
        this.$store.dispatch('user/login', { username, password })
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.login-container{
  display: flex;
  align-items: stretch;
  height: 100vh;
  .logo {
    flex: 4;
    background: rgba(38, 72, 176) url(../../assets/common/login_back.png) no-repeat center / cover;
    border-top-right-radius: 60px;
  }
  .form {
    flex: 2;
    display: flex;
    flex-direction: column;
    justify-content: center;
    padding-left: 176px;

    .el-card {
      border: none;
      padding: 0;
      width: 350px;
    }

    h1 {
      padding-left: 20px;
      padding-top: 180px;
      padding-bottom: 30px;
      font-size: 24px;
      font-weight: normal
    }

    // .el-input {
    //   width: 350px;
    //   height: 44px;
    //   .el-input__inner {
    //     background: #f4f5fb;
    //   }
    // }

   .el-checkbox {
      color: #606266;
    }

    // .el-form-item__label {
    //   font-size: 16px;
    //   font-weight: normal;
    // }
  }
}
</style>
